package storage;


import general.Flag;

import java.util.Properties;

import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
/**
 * тесты для Storage и backend'а
 * 
 * @author Last G
 * 
 */
public class StorageTests
{
	private Storage stor = null;
	@Test
	public void testMemory()
	{
		Properties config = new Properties();
		config.setProperty("storage.type", "MEMORY");
		
		runTest(config);
		
	}
	@Ignore("We don't use QuickDB anymore")
	@Test
	public void testQuickDB()
	{
		Properties config = new Properties();
		config.setProperty("storage.type", "QuickDB");
		config.setProperty("storage.db.type", "sqlite");
		config.setProperty("storage.db.name", "test.db");
		
		runTest(config);
	}
	@Test
	public void testORMLite()
	{
		Properties config = new Properties();
		config.setProperty("storage.type", "ORMLite");
		config.setProperty("storage.db.jdbc", "jdbc:derby:test;create=true");
		
		runTest(config);
	}
	void runTest(Properties config)
	{
		try
		{
			stor = new Storage(config);
			stor.flushDB();
			mainTestSuite();
		} catch (Exception e)
		{
			e.printStackTrace();
			fail();
		}
	}
	void mainTestSuite() throws StorageException
	{
		oneFlag();
		duplicateFlags();
	}
	
	void oneFlag() throws StorageException
	{
		Flag x = new Flag("Trololo", "Tralala", "Nibbles", "12.0.0.1");
		int id = stor.putFlag(x);
		assertTrue("Bad ID:"+ id,id >= 0);
		assertEquals(x, stor.getFlag(id));
	}
	void duplicateFlags() throws StorageException
	{
		Flag x = new Flag("xx", "Tralala", "Nibbles", "12.0.0.1");
		Flag y = new Flag("xx", "qwer", "qwer", "12.0.0.2");
		
		int idX = stor.putFlag(x);
		int idY = stor.putFlag(y);
		assertEquals("Can be duplicate flags", idX, idY);
	}
}
